<?php

namespace dragon\Config;

use think\Model;

class BaseConfigModel extends Model
{

    protected $name = 'base_config';

    /**
     * @param array $data     数据
     * @param array $where    更新条件
     * @param null  $sequence 自增序列名
     *
     * @return false|int
     */
    public function save($data = [], $where = [], $sequence = null)
    {
        parent::save($data, $where, $sequence);
        return $this->setConfig();
    }


    /**
     * 删除记录
     *
     * @access public
     *
     * @param mixed $data 主键列表 支持闭包查询条件
     *
     * @return integer 成功删除的记录数
     */
    public static function destroy($data)
    {
        parent::destroy($data);
        return self::setConfig();
    }

    /**
     *   更新缓存
     */
    public static function setConfig()
    {
        $list = static::all();
        $_list = [];
        foreach ($list as $val) {
            $_list[$val['key']] = $val['val'];
        }
        $data = "<?php \r\n return\t" . var_export($_list, true) . ";";

        file_put_contents(DC_ROOT . 'data/conf/base.php', $data);
    }


}
